package AddressService

import (
	"context"
	"database/sql"
	"gdshop-back-end-go/client-api/app/response/AddressResp"
	toolsDb "gdshop-back-end-go/common/db"
	"gdshop-back-end-go/common/entity"
	"gdshop-back-end-go/common/request/AddressReq"
	"gdshop-back-end-go/common/response"
	"github.com/gogf/gf/frame/g"
	"time"
)

func getModel(addressId int) {

}

func GetMyAddressModel(ctx context.Context, addressId int, memberId int) *AddressResp.Item {
	addressModel := AddressResp.Item{}
	err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "member_address").Where(
		"id=? AND member_id=?",
		addressId,
		memberId,
	).Struct(&addressModel)

	if err != nil {
		if err != sql.ErrNoRows {
			g.Log().Async().Error("AddressService.GetMemberModel失败：" + err.Error())
			return nil
		}
	}

	return &addressModel
}

func doSave(ctx context.Context, parames *AddressReq.Save, typeString string) *response.JsonResponse {
	if typeString == "add" {
		r, err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "member_address").Insert(g.Map{
			"member_id":    parames.MemberId,
			"addressee":    parames.Addressee,
			"mobile":       parames.Mobile,
			"province":     parames.Province,
			"city":         parames.City,
			"area":         parames.Area,
			"street":       parames.Street,
			"address":      parames.Address,
			"is_default":   parames.IsDefault,
			"address_code": parames.AddressCode,
			"create_at":    time.Now().Unix(),
		})
		if err != nil {
			return response.FailByRequestMessage(nil, err.Error())
		}
		newId, rerr := r.LastInsertId()
		if rerr != nil {
			return response.FailByRequestMessage(nil, rerr.Error())
		}
		return response.SuccessByRequestMessageData(nil, "添加成功", newId)
	} else {
		// 修改
		addressModel := (*entity.MemberAddress)(nil)
		err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "member_address").Where(
			"id=?",
			parames.Id,
		).Struct(&addressModel)
		if err != nil {
			if err != sql.ErrNoRows {
				// 不是为空，返回错误
				return response.FailByRequestMessage(nil, err.Error())
			}
		}
		if addressModel == nil {
			return response.FailByRequestMessage(nil, "找不到数据")
		}

		_, err = toolsDb.GetUnSafaTableAddDeleteWhere(
			ctx,
			"member_address",
		).Where("id=?", parames.Id).Update(g.Map{
			"addressee":    parames.Addressee,
			"mobile":       parames.Mobile,
			"province":     parames.Province,
			"city":         parames.City,
			"area":         parames.Area,
			"street":       parames.Street,
			"address":      parames.Address,
			"is_default":   parames.IsDefault,
			"address_code": parames.AddressCode,
			"update_at":    time.Now().Unix(),
		})
		if err != nil {
			return response.FailByRequestMessage(nil, "找不到数据")
		}

		return response.SuccessByRequestMessage(nil, "编辑成功")
	}
}

func GetMemberDefaultAddress(ctx context.Context, memberId int) *AddressResp.Item {
	addressModel := AddressResp.Item{}
	err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "member_address").Where(
		"member_id=? AND is_default = 1",
		memberId,
	).Order("update_at DESC,create_at DESC,id DESC").Struct(&addressModel)

	if err != nil {
		g.Log().Async().Error("AddressService.GetMemberDefaultAddress失败：" + err.Error())
		return nil
	}

	return &addressModel
}

func GetMemberAddressById(ctx context.Context, memberId int, AddressId int) *AddressResp.Item {
	addressModel := AddressResp.Item{}
	err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "member_address").Where(
		"member_id=? AND id = ?",
		memberId, AddressId,
	).Order("update_at DESC,create_at DESC,id DESC").Struct(&addressModel)

	if err != nil {
		g.Log().Async().Error("GetMemberAddressById 失败：" + err.Error())
		return nil
	}

	return &addressModel
}
